package controller

import (
	"intelli-dl-online-logo-go/model"
	"intelli-dl-online-logo-go/service"
	"intelli-dl-online-logo-go/utils"
	"net/http"

	"github.com/gin-gonic/gin"
)

type UserController struct {
	userService service.UserService
}

// LoginRequest 登录请求结构
type LoginRequest struct {
	Username string `json:"username" binding:"required"`
	Password string `json:"password" binding:"required"`
}

// LoginResponse 登录响应结构
type LoginResponse struct {
	Token    string `json:"token"`
	UserID   string `json:"user_id"`
	Username string `json:"username"`
	Email    string `json:"email"`
}

func NewUserController() *UserController {
	return &UserController{
		userService: service.NewUserService(),
	}
}

func (uc *UserController) CreateUser(c *gin.Context) {
	var req model.UserRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, utils.ErrorResponse(utils.ErrInvalidUserInput))
		return
	}

	user, err := uc.userService.CreateUser(req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, utils.ErrorResponse(utils.ErrUserCreateFailed))
		return
	}

	c.JSON(http.StatusOK, model.UserResponse{
		ID:       user.ID,
		Username: user.Username,
		Email:    user.Email,
	})
}

func (uc *UserController) GetUser(c *gin.Context) {
	id := c.Param("id")
	user, err := uc.userService.GetUser(id)
	if err != nil {
		c.JSON(http.StatusInternalServerError, utils.ErrorResponse(utils.ErrUserGetFailed))
		return
	}

	c.JSON(http.StatusOK, model.UserResponse{
		ID:       user.ID,
		Username: user.Username,
		Email:    user.Email,
	})
}

func (uc *UserController) UpdateUser(c *gin.Context) {
	id := c.Param("id")
	var req model.UserRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, utils.ErrorResponse(utils.ErrInvalidUserInput))
		return
	}

	user, err := uc.userService.UpdateUser(id, req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, utils.ErrorResponse(utils.ErrUserUpdateFailed))
		return
	}

	c.JSON(http.StatusOK, model.UserResponse{
		ID:       user.ID,
		Username: user.Username,
		Email:    user.Email,
	})
}

func (uc *UserController) DeleteUser(c *gin.Context) {
	id := c.Param("id")
	if err := uc.userService.DeleteUser(id); err != nil {
		c.JSON(http.StatusInternalServerError, utils.ErrorResponse(utils.ErrUserDeleteFailed))
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": utils.ErrUserDeleteSuccess})
}

// Login 用户登录
func (uc *UserController) Login(c *gin.Context) {
	var req LoginRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, utils.ErrorResponse(utils.ErrInvalidUserInput))
		return
	}

	// 验证用户名和密码
	user, err := uc.userService.VerifyUser(req.Username, req.Password)
	if err != nil {
		c.JSON(http.StatusUnauthorized, utils.ErrorResponse(utils.ErrLoginFailed))
		return
	}

	// 生成token
	token, err := utils.GenerateToken(user.ID, user.Username, user.Email)
	if err != nil {
		c.JSON(http.StatusInternalServerError, utils.ErrorResponse("生成token失败"))
		return
	}

	// 返回登录成功信息
	c.JSON(http.StatusOK, LoginResponse{
		Token:    token,
		UserID:   user.ID,
		Username: user.Username,
		Email:    user.Email,
	})
}

// Logout 用户登出
func (uc *UserController) Logout(c *gin.Context) {
	userID := c.GetString("userID")
	if userID == "" {
		c.JSON(http.StatusBadRequest, utils.ErrorResponse("用户ID不能为空"))
		return
	}

	// 从Redis中删除token
	err := utils.RemoveToken(userID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, utils.ErrorResponse("登出失败"))
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": utils.ErrLogoutSuccess})
}
